package lichunUtil;

public class SimpleStatistics {
//	static public double kurtosis(double[] data) {
//		double m = mean(data, 4);
//		double s = standarddeviation(data);
//		return m/Math.pow(s, 4) -3;
//	}
//	static public double kurtosis(int[] data) {
//		double m = mean(data, 4);
//		double s = standarddeviation(data);
//		return m/Math.pow(s, 4) -3;
//	}
	
	static public double calcCosSim (double[] a1, double[] a2) {
		double dot=0, ma1=0, ma2=0;
		if (a1.length!=a2.length) 
			System.err.println("length error");
		for (int i=0;i<a1.length;i++) {
			dot += a1[i] * a2[i];
			ma1 += a1[i] * a1[i];
			ma2 += a2[i] * a2[i];
		}
		
		return dot/Math.sqrt(ma1)/Math.sqrt(ma2);
	}
	static public double calcCosSim (int[] a1, int[] a2) {
		double dot=0, ma1=0, ma2=0;
		if (a1.length!=a2.length) 
			System.err.println("length error");
		for (int i=0;i<a1.length;i++) {
			dot += a1[i] * a2[i];
			ma1 += a1[i] * a1[i];
			ma2 += a2[i] * a2[i];
		}
		if (ma1==0||ma2==0) {
			System.err.println("zero vector");
			return 0;
		}
		return dot/Math.sqrt(ma1)/Math.sqrt(ma2);
	}
	static public double mean (double[] data, double pow) {
		if (data.length==0)
			System.err.println("data length 0");
		double m =0;
		for (double x: data)
			m += Math.pow(x, pow);
		return m/data.length;
	}
	static public double mean (double[] data) {
		if (data.length==0)
			System.err.println("data length 0");
		double m =0;
		for (double x: data)
			m +=x;
		return m/data.length;
	}
	static public double mean (int[] data, double pow) {
		if (data.length==0)
			System.err.println("data length 0");
		double m =0;
		for (double x: data)
			m += Math.pow(x, pow);
		return m/data.length;
	}
	static public double mean (int[] data) {
		if (data.length==0)
			System.err.println("data length 0");
		double m =0;
		for (int x: data)
			m +=x;
		return m/data.length;
	}
	static public double standarddeviation (int[] data) {
		return standarddeviation(data, mean(data));
	}
	static public double standarddeviation (int[] data, double mean) {
		double r =0;
		for (int x : data) {
			r += (mean-x)*(mean-x);
		}
		return Math.sqrt(r/data.length);
	}
	static public double standarddeviation (double[] data) {
		return standarddeviation(data, mean(data));
	}
	static public double standarddeviation (double[] data, double mean) {
		double r =0;
		for (double x : data) {
			r += (mean-x)*(mean-x);
		}
		return Math.sqrt(r/data.length);
	}
	static public double entropy (int[] weightings) {
		double sum = 0;
		for (int x : weightings)
			sum += x;
		if (sum==0)
			return Double.NaN;
		double e =0;
		for (int i=0;i<weightings.length;i++) {
			double p = 1.0*weightings[i]/sum;
			if (p>0)
				e-=p*Math.log(p);
		}
		return e;
	}
	static public double entropy (double[] weightings) {
		double sum = 0;
		for (double x : weightings)
			sum += x;
		double e =0;
		for (int i=0;i<weightings.length;i++) {
			double p = 1.0*weightings[i]/sum;
			e-=p*Math.log(p);
		}
		return e;
	}
	
	static public void selftest() {
		int[]x =new int[]{3,7,7,19};
		System.out.println(standarddeviation(x));
	}
}